﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using ZFine.Code;

namespace ZFine.Application.Project
{
    public class PersonSalaryApp
    {
        private IPersonSalaryRepository service = new PersonSalaryRepository();
        public List<PersonalSalaryEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public List<PersonalSalaryEntity> GetList(Expression<Func<PersonalSalaryEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }
        public List<PersonalSalaryEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<PersonalSalaryEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }

        public List<PersonalSalaryEntity> GetYunLiangList(Pagination pagination, string keyword,string StartTime,string EndTime)
        {
            //List<PersonalSalaryEntity> res = new List<PersonalSalaryEntity>();
            var expression = ExtLinq.True<PersonalSalaryEntity>();
            if (string.IsNullOrEmpty(keyword))
            {
                return null;
            }
            else
            {
                expression = expression.And(f => f.Persion.F_Id == keyword);
                expression = expression.And(f=>f.State==true);
                expression = expression.And(f => f.SalaryType == 0);
                if (!string.IsNullOrEmpty(StartTime))
                {
                    DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        DateTime eTime = Convert.ToDateTime(EndTime);
                        expression = expression.And(f=>f.OperationTime>=sTime);
                        expression = expression.And(f => f.OperationTime < eTime);
                    }
                    else
                    {
                        expression = expression.And(f => f.OperationTime >=sTime);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        DateTime eTime = Convert.ToDateTime(EndTime);
                        expression = expression.And(f => f.OperationTime < eTime);
                    }
                   
                }
            }
            var result = service.FindList(expression, pagination);
            return result;
        }
        public List<PersonalSalaryEntity> GetFangXingList(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            //List<PersonalSalaryEntity> res = new List<PersonalSalaryEntity>();
            var expression = ExtLinq.True<PersonalSalaryEntity>();
            if (string.IsNullOrEmpty(keyword))
            {
                return null;
            }
            else
            {
                expression = expression.And(f => f.Persion.F_Id == keyword);
                expression = expression.And(f => f.State == true);
                expression = expression.And(f => f.SalaryType == 1);
                if (!string.IsNullOrEmpty(StartTime))
                {
                    DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        DateTime eTime = Convert.ToDateTime(EndTime);
                        expression = expression.And(f => f.OperationTime >= sTime);
                        expression = expression.And(f => f.OperationTime < eTime);
                    }
                    else
                    {
                        expression = expression.And(f => f.OperationTime >= sTime);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        DateTime eTime = Convert.ToDateTime(EndTime);
                        expression = expression.And(f => f.OperationTime < eTime);
                    }

                }
            }
            var result = service.FindList(expression, pagination);
            return result;
        }
        public List<PersonalSalaryEntity> GetListD(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<PersonalSalaryEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public PersonalSalaryEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }
        public int SubmitForm(PersonalSalaryEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.Modify(keyValue);
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }

        public List<PersonalSalaryEntity> GetDuiweiList(Pagination pagination, string keyword, string StartTime, string EndTime)
        {
            //List<PersonalSalaryEntity> res = new List<PersonalSalaryEntity>();
            var expression = ExtLinq.True<PersonalSalaryEntity>();
            if (string.IsNullOrEmpty(keyword))
            {
                return null;
            }
            else
            {
                expression = expression.And(f => f.Persion.F_Id == keyword);
                expression = expression.And(f => f.SalaryType == 2);
                expression = expression.And(f => f.State == true);
                if (!string.IsNullOrEmpty(StartTime))
                {
                    DateTime sTime = Convert.ToDateTime(StartTime);
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        DateTime eTime = Convert.ToDateTime(EndTime);
                        expression = expression.And(f => f.OperationTime >= sTime);
                        expression = expression.And(f => f.OperationTime < eTime);
                    }
                    else
                    {
                        expression = expression.And(f => f.OperationTime >= sTime);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        DateTime eTime = Convert.ToDateTime(EndTime);
                        expression = expression.And(f => f.OperationTime < eTime);
                    }

                }
            }
            var result = service.FindList(expression, pagination);
            return result;
        }

    }
}
